package com.ljj.examisto.widget

import android.app.PendingIntent
import android.appwidget.AppWidgetManager
import android.appwidget.AppWidgetProvider
import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.util.Log
import android.widget.RemoteViews
import com.ljj.examisto.R
import com.ljj.examisto.ui.activity.HomeActivity
import com.ljj.examisto.util.Logger.TAG

/**
 * 考试倒计时小组件实现类
 */
class ExamCountdownWidget : AppWidgetProvider() {
    companion object {
        const val ACTION_APPWIDGET_RESET = "com.ljj.examisto.APPWIDGET_RESET"
    }

    override fun onReceive(context: Context, intent: Intent?) {
        super.onReceive(context, intent)
        // 获取AppWidgetManager实例和当前小组件的ID
        val appWidgetManager = AppWidgetManager.getInstance(context)
        val thisWidget = ComponentName(context, ExamCountdownWidget::class.java)
        val appWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget)
        // 处理系统广播更新
        when (intent?.action) {
            AppWidgetManager.ACTION_APPWIDGET_UPDATE -> {
                // 处理更新小组件广播
                val examName = intent.getStringExtra("examName")
                val timeLeft = intent.getStringExtra("timeLeft")
                if (appWidgetIds.isNotEmpty()) {
                    for (appWidgetId in appWidgetIds) {
                        update(context, appWidgetManager, appWidgetId, examName, timeLeft)
                    }
                }
            }
            ACTION_APPWIDGET_RESET -> {
                // 处理重置小组件广播
                if (appWidgetIds.isNotEmpty()) {
                    for (appWidgetId in appWidgetIds) {
                        reset(context, appWidgetManager, appWidgetId)
                    }
                }
            }
            else -> {
                // 处理其他广播
                Log.d(TAG, "小组件收到未知广播: ${intent?.action}")
            }
        }
    }

    private fun update(
        context: Context,
        appWidgetManager: AppWidgetManager,
        appWidgetId: Int,
        examName: String?,
        timeLeft: String?
    ) {
        val views = RemoteViews(context.packageName, R.layout.widget_exam_countdown)
        // 设置点击事件
        setupClickIntent(context, views)
        // 更新小组件内容
        views.setTextViewText(R.id.tv_exam_name, examName)
        views.setTextViewText(R.id.tv_countdown, timeLeft)
        // 告诉AppWidgetManager更新小组件
        appWidgetManager.updateAppWidget(appWidgetId, views)
    }

    private fun reset(context: Context, appWidgetManager: AppWidgetManager, appWidgetId: Int) {
        val views = RemoteViews(context.packageName, R.layout.widget_exam_countdown)
        // 重置小组件内容
        views.setTextViewText(R.id.tv_exam_name, "Examisto")
        views.setTextViewText(R.id.tv_countdown, "LJJ")
        // 更新小组件
        appWidgetManager.updateAppWidget(appWidgetId, views)
    }

    // 设置点击事件，点击小组件打开主应用
    private fun setupClickIntent(context: Context, views: RemoteViews) {
        val pendingIntent = PendingIntent.getActivity(
            context,
            0,
            Intent(context, HomeActivity::class.java),
            PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
        )
        views.setOnClickPendingIntent(R.id.ll_widget_area, pendingIntent)
    }
}
